#!/bin/sh

####################################
#  254  测试common-info发布脚本     #

#-----------------------------------
#    g:选择分支（default：dev）
#-----------------------------------

while getopts "g:" opt
do
    case $opt in
    g) #set option "g"
          branch=$OPTARG
          ;;
       *)
           echo "-$opt not recognized ,choose default branch 'dev'"
           ;;
    esac
done

echo "选择的分支为 ：${branch}"

#------  声明发布项目名称  ------#

declare appName=common-info;

#------  指定源码所在目录  ------#

declare appPath=/home/working/${appName};

#------  项目打包maven所在目录  ------#

declare server_sub_dir=${appPath}/${appName}-server;

#------  打包后项目jar包名称  ------#

declare zipFile=${appName}-server-1.0-SNAPSHOT-cli.tar.gz;

#------  打包后项目jar包路径  ------#

declare source_zip_file=${appPath}/${appName}-server/target/${zipFile};

#------  打包class 文件所在路径

declare DEPLOY_APP_PATH=/home/app/server/${appName};

#------  启动服务命令所在目录  ------#

declare DEPLOY_DIR=/home/bin/${appName};

#------  目录定义完毕  ------#

declare path=$(cd `dirname $0`; pwd)

echo "code path is :" ${appPath}

#------  移除maven仓库中旧的package  ------#

rm -rf /root/.m2/repository/com/zs/server/wechat-server
rm -rf /root/.m2/repository/com/zs/server/wechat-api
rm -rf /root/.m2/repository/com/zs/server/wechat
rm -rf /root/.m2/repository/com/lbiuse/
rm -rf /root/.m2/repository/org/bidtime/utils/
echo "移除仓库jar完成."

cd ${appPath}

echo "current path is :" ${appPath}

#echo "选择Git 分支 开始："

if [ -n "${branch}" ];
    then
    echo "git 分支 {branch}"
    git chechout ${branch}
    git pull origin ${branch}
else
    echo  "git 分支............... 【dev】"
    git checkout dev
    git pull origin dev
fi

#echo "git pull end."

echo "mvn clean install start ... ..."
mvn clean install
# mvn clean install -Dmaven.test.skip=true
echo "mvn clean install end."

#assemb
    echo "开始装配 。。。"
    #  cd server sub dir
    echo "开始server 建包:"${server_sub_dir}
    cd ${server_sub_dir}
    mvn assembly:assembly
    cd ..
    echo "装配完毕。。。"


if [ ! -f "${source_zip_file}" ];
    then
       echo "source file ${source_zip_file} is not exist,exit"
       exit 0;
fi

#------ everything is ready ,deploying
echo "${appName} starting to deploy..."


# deploy
declare deploy_arch_path=${DEPLOY_APP_PATH}/archv;
if [ ! -d "${deploy_arch_path}" ];
then
    echo "context【${deploy_arth_path}】 is not exist ,create it"
    mkdir -p ${deploy_arch_path}
fi

declare deploy_zip_file=${source_arch_path}/${zipFile}
if [ ! -f "${deploy_zip_file}" ];
    then 
       echo "zip source exist【${deploy_zip_file}, delete it】"
       rm -rf ${deploy_zip_file}
fi

mv -f ${source_zip_file} ${deploy_zip_file}





if [ ! -f "${deploy_zip_file}" ]
   then
    echo "source 【${deploy_zip_file}】 doesn't exist,must exit"
    exit 0;
   else 
    echo "source file 【${deploy_zip_file}】exist,can next."
    echo "it is all okay！"
fi

echo "deploy ${appName}
${DEPLOY_DIR}/deploy.sh ${appName}

echo "******************* deploy end ************************"

echo "${appName} end deploy."

cd ${path}

